import { aspects } from '../event/aspects'
import {Water} from '../Object3D/Water'
import { Vector3 } from '../Math/Vector3'
import { WaterMaterial } from '../Material/WaterMaterial'
const waterlist: Array<Water> = [];
export class drawWater {
    readonly type: string
    material: WaterMaterial
    constructor(waterMaterial: WaterMaterial = new WaterMaterial()){ 
        this.type = 'Water'
        this.material = waterMaterial
    }
    
    // 绘制水面
    draw() {
        aspects.multipoint(false,(data: any) => { 
        const list = data.pointList
        this.addWaterPolygon(list)
    })
}
    private addWaterPolygon(list:Array<Vector3>) {
        let maxZ = -Infinity
        let minZ = Infinity
        for (const point of list) {
            if (point.z > maxZ) {
                maxZ = point.z
            }
            if (point.z < minZ) {
                minZ = point.z
            }
        }
        const BaseHeight = minZ - 1
        const TargetHeight = maxZ + 1
        const newWater = new Water('shui', BaseHeight, TargetHeight)
        waterlist.push(newWater)
        newWater.setWaterMaterial(this.material)
        newWater.buildByList(list)
        aspects.removeAspects()
    }
    //移除水面
    removeWater() {
        for (const WaterPolygon of waterlist) {
            WaterPolygon.delete()
        }
        waterlist.splice(0, waterlist.length)
        
    }
    //更新水面颜色
    newWaterMaterial(WaterMaterial:WaterMaterial) {
        for (const WaterPolygon of waterlist) {
            WaterPolygon.replaceWaterMaterial(WaterMaterial)
        }
    }
    // 停止绘制
    stopWater() {
        aspects.stopTwoAspects()
    }
}

